﻿/*
    File Name:      uknow_url_mapping.js
    Created By:     victor.liu
    Created When:   2012-03-05
    Created Whys:   This js file is created mainly for url mapping on static html page
*/

var mapping_array = [
    "about.html",
    "events.html",
    "collaboration.html",
    "britishcouncil.html",
    "eventslistings.html",
    "interactivespace.aspx",
    "introduction.html",
    "winners.html",
    "sponsors.html",
    "brunswick.html",
    "burberry.html",
    "diagoe.html",
    "hsbc.html",
    "ihg.html",
    "jlr.html",
    "nortonrose.html",
    "prudential.html",
    "standardchartered.html",
    "swire.html",
    "ielts.html",
    "sponsoruknow.aspx",
    "search.html",
    "home.html",
    "sitemap.html",
    "pressroom.html",
    "contactus.html",
    "sitemap.html",
    "gallery.html",
    "register.html",
    "modernmedia.html",
    "selector.html"
];

function existedInMapping(location) {
    var url = location.href;
    var lastIndex = url.lastIndexOf("/");
    var pageName = url.substr(lastIndex + 1);
    var questionIndex = pageName.indexOf("?");
    if (questionIndex > -1)
        pageName = pageName.substring(0, questionIndex);
    var length = mapping_array.length;
    for (var i = 0; i < length; i++) {
        if (pageName.toLowerCase() == mapping_array[i]) {
            return mapping_array[i];
        }
    }

    return null;
}

function cnenlink() {
    var hdn = document.getElementById("hdn_eventsId"); //此JS文件已引用到要映射的页面,,找到某一类型的对象的id-----hdn_eventsId
    var hdnNewsID = document.getElementById("hdn_newsId");
    var type = null;
    var id = null;
    var url = null;
    var defUrl = null;
    if (hdn) {
        type = "1";
        id = hdn.value;
        url = "/en/events/";
        defUrl = url + "eventslistings.html";
    }
    if (hdnNewsID) {
        type = "2";
        id = hdnNewsID.value;
        url = "/en/news/";
        defUrl = url + "pressroom.html";
    }

    if (type) {
        $.post("/CnEnMapp.aspx",
            { typeID: type, lanid: "2052", eventid: id },
            function (data) {
                if (data != "error" && data.toString() != "0") {
                    // 活动增加SEO功能
                    if (type == "1") {
                        // 判断当前是不是巡回教育活动页面
                        var fileName = "";
                        var hdnRound = document.getElementById("hdn_roundEventsId");
                        if (hdnRound) {
                            var reqUrl = Events.gateUrl + '?op=GetRoundEventsHtmlFileName&lang=1033&eid=' + data;
                            var req = new ajaxRequest(reqUrl);
                            req.async = false;
                            req.get(function (res) {
                                fileName = res;
                            });
                            if (fileName != "") fileName = url + fileName;
                        } else {
                            // 以下函数需要引用/js/uknow_events.js文件
                            var evtModel = Events.getModelByID(1033, data);
                            if (evtModel == null) return;
                            fileName = Events.getFileName(evtModel);
                        }
                        //alert(fileName);
                        if (fileName == "") fileName = defUrl;

                        $("#toenlink").attr("href", fileName);
                    } else {
                        $("#toenlink").attr("href", url + data + ".html");
                    }
                }
                else {
                    $("#toenlink").attr("href", defUrl);
                }
            });
    }
    else {
        var mappingUrl = existedInMapping(location);
        if (mappingUrl != null) {
            $("#toenlink").attr("href", location.href.replace("/cn/", "/en/"));
        }
    }
}
function encnlink() {
    var hdn = document.getElementById("hdn_eventsId");
    var hdnNewsID = document.getElementById("hdn_newsId");
    var type = null;
    var id = null;
    var url = null;
    var defUrl = null;
    if (hdn) {
        type = "1";
        id = hdn.value;
        url = "/cn/events/";
        defUrl = url + "eventslistings.html";
    }
    if (hdnNewsID) {
        type = "2";
        id = hdnNewsID.value;
        url = "/cn/news/";
        defUrl = url + "pressroom.html";
    }
    if (type) {

        $.post("/CnEnMapp.aspx", { typeID:type ,lanid: "1033", eventid: id },
            function (data) {
                if (data != "error" && data.toString() != "0") {
                    // 活动增加SEO功能
                    if (type == "1") {
                        var fileName = "";
                        var hdnRound = document.getElementById("hdn_roundEventsId");
                        if (hdnRound) {
                            var reqUrl = Events.gateUrl + '?op=GetRoundEventsHtmlFileName&lang=2052&eid=' + data;
                            var req = new ajaxRequest(reqUrl);
                            req.async = false;
                            req.get(function (res) {
                                fileName = res;
                            });
                            if (fileName != "") fileName = url + fileName;
                        } else {
                            // 以下函数需要引用/js/uknow_events.js文件
                            var evtModel = Events.getModelByID(2052, data);
                            if (evtModel == null) return;
                            fileName = Events.getFileName(evtModel);
                        }
                        //alert(fileName);
                        if (fileName == "") fileName = defUrl;

                        $("#toenlink").attr("href", fileName);
                    } else {
                        $("#toenlink").attr("href", url + data + ".html");
                    }
                }
                else {
                    $("#toenlink").attr("href", defUrl);
                }
            }
        );
    } else {
        var mappingUrl = existedInMapping(location);
        if (mappingUrl != null) {
            $("#toenlink").attr("href", location.href.replace("/en/", "/cn/"));
        }
    }
}
